$("#count").bootstrapTable({
    url: "/count/list",
    method: "get",
    pagination: true,   //是否显示分页（*）
    pageSize: 10,       //每页的记录行数（*）
    pageList: [10, 20, 50, 100, 'All'], //可供选择的每页的行数（*）
    sidePagination: "client",    //分页方式：client客户端分页，server服务端分页（*）
    pageNumber: 1,                    //初始化加载第一页，默认第一页
    classes: "table table-bordered table-striped table-condensed table-hover",
    //请求服务数据时所传查询参数
    queryParams: function (params) {
        return {
            dateType: $("#search-dateType").val(),
            InOutType: $("#search-inOutType").val(),
        };
    },
    // 动态修改行的背景颜色
    rowStyle: function (row, index) { //row：行对象， index ：第几行
        if (row.inOutType == 1) {
            return {css: {"background-color": 'rgb(127,255,0)'}};
        } else if (row.inOutType == 2) {
            //预警库存不为0，且库存低于预警库存
            return {css: {"background-color": 'rgb(218,112,214)'}};
        }
    },
    columns: [{
        title: "时间",
        field: "dateRange",
    }, {
        title: "商品名称",
        field: "goodsName",
    }, {
        title: "盒装数量",
        field: "boxNum",
    }, {
        title: "数量",
        field: "num",
    },{
        title: "入库成本：元",
        field: "priceYuan",
    }, {
        title: "供应商或店铺名称",
        field: "supplierOrShopName",
    }]
});

$("#search-startAndEndTime").daterangepicker({
    locale: {
        "format": 'YYYY-MM-DD HH:mm:ss',
        "separator": " - ",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "resetLabel": "重置",
        "fromLabel": "起始时间",
        "toLabel": "结束时间'",
        "customRangeLabel": "自定义",
        "weekLabel": "W",
        "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
        "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        "firstDay": 1
    },
    ranges: {
        '今日': [moment(), moment()],
        '昨日': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        '最近7日': [moment().subtract(6, 'days'), moment()],
        '最近30日': [moment().subtract(29, 'days'), moment()],
        '本月': [moment().startOf('month'), moment().endOf('month')],
        '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
        '今年': [moment().startOf('year'), moment().endOf('year')],
    },
    "alwaysShowCalendars": true,
    "startDate": moment().subtract(1, 'years'),  //1年前
    "endDate": new Date(),
    "opens": "right",
}, function (start, end, label) {
});

function searchCountOnClick() {
    let startAndEndTime = $("#search-startAndEndTime").val().replace(/\s-\s/ig, '?').split('?');
    $.get({
        url: "/count/list",
        data: {
            goodsName: $("#search-goodsName").val(),
            supplierOrShopName: $("#search-supplierOrShopName").val(),
            startDateTime: startAndEndTime[0],
            endDateTime: startAndEndTime[1],
            dateType: $("#search-dateType").val(),
            inOutType: $("#search-inOutType").val(),
            isVerified: $("#search-verifiedType").val()
        },
        dataType: "json",
        success: function (json) {
            $("#count").bootstrapTable('load', json);
        }
    });
}

//按下Enter查询
$(document).keyup(function (event) {
    if (event.keyCode == 13){
        searchCountOnClick();
    }

});

$("#export").click(function () {
    let startAndEndTime = $("#search-startAndEndTime").val().replace(/\s-\s/ig, '?').split('?');
    $.get({
        url: "/count/export",
        data: {
            goodsName: $("#search-goodsName").val(),
            supplierName: $("#search-supplierName").val(),
            shopName: $("#search-shopName").val(),
            startDateTime: startAndEndTime[0],
            endDateTime: startAndEndTime[1],
            dateType: $("#search-dateType").val(),
            inOutType: $("#search-inOutType").val(),
            isVerified: $("#search-verifiedType").val()
        },
        dataType: "json",
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("导出成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
});

$("#reset").click(function () {
    $("#search-goodsName").val("");
    $("#search-supplierOrShopName").val("");
    $("#search-startAndEndTime").val("");
    $("#search-dateType").val("DATE");
    $("#search-inOutType").val(0);
    $("#search-verifiedType").val("");
});


